import { request } from '../../requests'
import { buildPostApi } from '../index'
import { buildGetApi } from '../index'

const wechatAuth = '/wechat/auth'

export const getWechatAuthJsApi = (baseUrl = window.location.href.split('#')[0]) =>
  request(buildGetApi(wechatAuth + '/jsApi'), { baseUrl }, {}, false, false) as Promise<any>

export const getWechatAuth = (path = '/home') =>
  request(
    buildGetApi(wechatAuth),
    { redirectUrl: window.location.href.split('#')[0] + '#' + path, state: 'login' },
    {},
    false,
    false
  ) as Promise<any>

export const authWechat = (code: string) =>
  request(buildPostApi(wechatAuth), { code }, {}, false, false) as Promise<any>

export const bindWxPhoneNumber = (phoneNumber: string, msgCode: string) =>
  request(buildPostApi('/wechat/reg'), { phoneNumber, msgCode }, {}, false, false) as Promise<any>
